package com.guanlixitong.dao.impl;

import com.guanlixitong.controller.impl.UserControllerImpl;
import com.guanlixitong.dao.IStudentDao;
import com.guanlixitong.dao.IUserDao;
import com.guanlixitong.factory.Factory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

public class UserDaoImpl implements IUserDao {

    private IStudentDao studentDao = Factory.getStudentDao();

    @Override//注册
    public void ZhuCePanDuan(String s, String username, String password, String phone) throws IOException {
        Scanner sc = new Scanner(System.in);
        int a = 1;
        l1:
        while (true) {
            System.out.println("请输入验证码：");

            String ad2 = sc.next();
            if (s.equals(ad2)) {
                BufferedReader fw5 = new BufferedReader(new FileReader("D:\\yonghu.txt",StandardCharsets.UTF_8));
                String s4;
                while ((s4 = fw5.readLine()) != null) {
                    String[] split = s4.split("======");
                    if (split[0].equals(username)) {
                        System.out.println("已存在该用户名，请重新注册");
                        a = 2;
                        break l1;
                    }
                }
                if (a == 1) {

                    System.out.println("注册成功");
                    FileWriter fw = new FileWriter("D:\\yonghu.txt", StandardCharsets.UTF_8,true);
                    fw.write("\r\n");
                    fw.write(username + "======" + password + "======" + phone);
                    fw.flush();
                    fw.close();
                    FileWriter fw2 = new FileWriter("D:\\rizhi.txt", StandardCharsets.UTF_8,true);
                    LocalDateTime now = LocalDateTime.now();
                    DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
                    String f1 = pattern.format(now);
                    fw2.write(username + " " + f1 + " " + "注册");
                    fw2.write("\r\n");
                    fw2.close();
                    break l1;
                }
                while ((s4 = fw5.readLine()) == null) {
                    System.out.println("注册成功");

                    //将用户信息写入用户文件
                    FileWriter fw1 = new FileWriter("D:\\yonghu.txt", StandardCharsets.UTF_8,true);
                    fw1.write(username + "*******" + password + "*******" + phone);
                    fw1.write("\r\n");
                    fw1.close();

                    //将注册信息写入日志
                    FileWriter fw2 = new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8, true);
                    LocalDateTime now = LocalDateTime.now();
                    DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
                    String f1 = pattern.format(now);
                    fw2.write(username + " " + f1 + " " + "注册");
                    fw2.write("\r\n");
                    fw2.close();
                    break l1;
                }
            } else {
                System.out.println("验证码错误");
                break l1;
            }
        }
    }

    @Override//修改密码
    public void XiuGaiMiMa(Scanner sc, String s, String number, BufferedReader fw1) throws IOException {

        System.out.println("请输入验证码：");
        String ad2 = sc.next();
        if (!s.equals(ad2)) {
            System.out.println("验证码错误");
            return;
        }
        BufferedReader br = new BufferedReader(new FileReader("D:\\yonghu.txt",StandardCharsets.UTF_8));
        ArrayList<String> list = new ArrayList<>();
        System.out.println("请输入忘记密码的用户名");
        String username = sc.next();
        System.out.println("请输入新的密码");
        String newpassword = sc.next();
        String len;
        while ((len = br.readLine()) != null) {
            String[] s1 = len.split("======");
            //只修改符合当前账号的密码
            if (s1[0].equals(username)) {
                s1[1] = newpassword;
                len = s1[0] + "======" + s1[1] + "======" + s1[2];
            }
            //修改后从新添加从文件中读出的数据,将数据添加到集合
            list.add(len);
        }

        br.close();

        //将文件的数据初始化,清空
        BufferedWriter bw1 = new BufferedWriter(new FileWriter("D:\\yonghu.txt"));
        bw1.write("");
        bw1.close();
        //遍历集合,将集合重新写入文件
        for (String s1 : list) {
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\yonghu.txt", true));
            bw.write(s1);
            bw.write("\r\n");
            bw.close();
        }
        System.out.println("修改成功");
        xiuGaiMiMaRiZhi(username);
    }

    @Override//登录
    public boolean loginpanduan(Scanner sc, int count) throws IOException, InterruptedException {
        String username;
        BufferedReader fw1 = new BufferedReader(new FileReader("D:\\yonghu.txt", StandardCharsets.UTF_8));
        String s2;
        if ((s2 = fw1.readLine()) != null) {
            li:
            while (true) {
                System.out.println("请输入用户名：");
                username = sc.next();
                System.out.println("请输入密码：");
                String password = sc.next();
                BufferedReader fw = new BufferedReader(new FileReader("D:\\yonghu.txt",StandardCharsets.UTF_8));
                String s1 = "";
                String[] split;

                while ((s1 = fw.readLine()) != null) {
                    split = s1.split("======");
                    if (split[0].equals(username) && split[1].equals(password)) {
                        BufferedReader buf2 = new BufferedReader(new FileReader("D:\\rizhi.txt",StandardCharsets.UTF_8));
                        String s;
                        ArrayList<String> list = new ArrayList<>();
                        while ((s = buf2.readLine()) != null) {
                            list.add(s);
                        }
                        if (split[0].equals(username)) {
                            for (int i = list.size() - 1; i >= 0; i--) {
                                String[] s3 = list.get(i).split(" ");
                                if (s3.length >= 3 && s3[3].equals("登录") && s3[0].equals(username)) {
                                    System.out.println("登录成功");
                                    System.out.println("欢迎" + username + "上一次登录时间为：" + s3[1] + s3[2]);

                                    studentDao.setUsername(username);
                                    LocalDateTime now = LocalDateTime.now();
                                    DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
                                    String f2 = pattern.format(now);
                                    FileWriter fil = new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8, true);
                                    fil.write(username + " ");
                                    fil.write(f2);
                                    fil.write(" " + "登录");
                                    fil.write("\r\n");
                                    fil.close();
                                    buf2.close();
                                    break li;
                                }
                            }

                            LocalDateTime now = LocalDateTime.now();
                            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
                            String f1 = pattern.format(now);
                            System.out.println("欢迎" + username + "当前登录时间为：" + f1);
                            FileWriter fil = new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8, true);
                            fil.write(username + " ");
                            fil.write(f1);
                            fil.write(" 登录");
                            fil.write("\r\n");
                            fil.close();
                            studentDao.setUsername(username);
                            break li;
                        } else {
                            break li;
                        }
                    }
                }
                fw.close();
                BufferedReader fw2 = new BufferedReader(new FileReader("D:\\yonghu.txt",StandardCharsets.UTF_8));
                String s8 = "";
                String[] split2 = new String[0];
                lii:
                while ((s8 = fw2.readLine()) != null) {
                    long firstDenglu = System.currentTimeMillis();
                    split2 = s8.split("======");
                    if (!split2[0].equals(username) || !split2[1].equals(password)) {
                        if (count <= 2) {
                            if (!split2[0].equals(username) && !split2[1].equals(password)) {
                                System.out.println("该用户不存在还有" + (3 - count) + "次机会");
                                count++;
                                break lii;
                            } else {
                                System.out.println("用户名或者密码错误，还有" + (3 - count) + "次机会");
                                count++;
                                break lii;
                            }
                        } else {
                            System.out.println("3次机会已经用完");
                            System.out.println("请20分钟后再登录");
                            LocalDateTime now1 = LocalDateTime.now();
                            DateTimeFormatter pattern1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
                            String f2 = pattern1.format(now1);
                            FileWriter f3 = new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8, true);
                            f3.write("未知用户 ");
                            f3.write(f2);
                            f3.write(" 登录失败");
                            f3.write("\r\n");
                            f3.flush();
                            f3.close();

                            long now = System.currentTimeMillis();

                            while ((now - firstDenglu) < 2 * 6 * 1000) {
                                String s = sc.nextLine();
                                now = System.currentTimeMillis();
                                System.out.println("请再等" + (1200 * 1000 - now + firstDenglu) / 1000 + "秒");
                            }

                            return false;
                        }
                    }
                }
            }
            return true;
        } else {
            System.out.println("请注册信息");
            UserControllerImpl userController = new UserControllerImpl();
            userController.ZhuCe();
            return false;
        }
    }

    //修改密码日志
    public void xiuGaiMiMaRiZhi(String username) throws IOException {
        /*BufferedReader br = new BufferedReader(new FileReader("D:\\rizhi.txt",StandardCharsets.UTF_8));
        String s;
        ArrayList<String> list = new ArrayList<>();
        while ((s = br.readLine()) != null&&"登录".equals(s.split(" ")[3])) {
            int i= 1;
            System.out.println(i++);
            list.add(s);
        }
        br.close();
        String s1 = list.get(list.size() - 1);

        String username = s1.split(" ")[0];*/

        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8,true));
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String f2 = pattern.format(now);
        bw.write(username + " " + f2 + " " +  "修改密码");
        bw.write("\r\n");
        bw.flush();
        bw.close();
    }
}
